package com.pfzy.utildemo.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Process;
import android.text.TextUtils;

import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;

public class ProcessUtil {
    private static String processName = null;

    public static boolean isMainProcess(Context context) {
        if (context == null) {
            context = CommonUtil.getContext();
        }
        String mainProcessName = null;
        ApplicationInfo applicationInfo = context.getApplicationInfo();
        if (applicationInfo != null) {
            mainProcessName = applicationInfo.processName;
        }
        if (TextUtils.isEmpty(mainProcessName)) {
            mainProcessName = context.getPackageName();
        }
        String currentProcessName = getCurrentProcessName(context);
        if (currentProcessName == null || currentProcessName.length() == 0) {
            currentProcessName = "";
        }
        return mainProcessName.equals(currentProcessName);
    }

    public static String getCurrentProcessName(Context context) {
        if (context == null) {
            context = CommonUtil.getContext();
        }
        if (!TextUtils.isEmpty(processName)) {
            return processName;
        }
        processName = getProcessNameInternal(context);
        return processName;
    }

    /**
     * 获取内部进程名称
     *
     * @param context
     * @return
     */
    private static String getProcessNameInternal(Context context) {
        FileInputStream in;
        int len;
        int myPid = Process.myPid();
        if (context == null || myPid <= 0) {
            return "";
        }

        ActivityManager.RunningAppProcessInfo myProcess = null;
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (activityManager != null) {
            try {
                List<ActivityManager.RunningAppProcessInfo> appProcessList = activityManager.getRunningAppProcesses();
                if (appProcessList != null) {
                    Iterator<ActivityManager.RunningAppProcessInfo> it = appProcessList.iterator();
                    while (it.hasNext()) {
                        ActivityManager.RunningAppProcessInfo process = it.next();
                        if (process.pid == myPid) {
                            myProcess = process;
                            break;
                        }
                    }
                    if (myProcess != null) {
                        return myProcess.processName;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        byte[] b = new byte[128];
        try {
            String str = "";
            in = new FileInputStream("/proc/" + myPid + "/cmdline");
            len = in.read(b);
            if (len > 0) {
                for (int i = 0; i < len; i++) {
                    if ((b[i] & 0xff) > 128 || b[i] <= 0) {
                        len = i;
                        break;
                    }
                }
                str = new String(b, 0, len);
            }

            if (in != null) {
                in.close();
            }
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Throwable th) {
            throw th;
        }
        return "";
    }
}
